var ctx;

function setCtx(ctx){
	this.ctx = ctx;
}
var isLogining = false;
function userLogin(){
	
	var userName = $("#userName").val();
	var pwd = $("#password").val();
	
	//解决ie9以下placeholder与value重复的问题
	if(userName == $("#userName").attr("placeholder")){
		userName = "";
	}
	if(pwd == $("#password").attr("placeholder")){
		pwd = "";
	}
	
	if (userName == '' || userName == undefined) {
		layer.tips('请输入用户名', '#userName', {
			tips: 3
		});
		return;
	}
	if (pwd == '' || pwd == undefined) {
		layer.tips('请输入密码!', '#password', {
			tips: 3
		});
		return;
	}
	
	pwd = encrypt(pwd,userName);
	
	var index = layer.load(1);
	$.get(ctx+"/login" + "?timestamp=" + Date.parse(new Date()),{
		uname: userName,
		pwd: pwd
	},function(data){
		data = JSON.parse(data);
		if(data.success){
			location.href = ctx + "/main";
		}else{
			layer.close(index);
			layer.msg(data.msg, {icon: 5});
		}
	})
}


function loginLoad(){ 
		$('#userName').focus();
		document.onkeydown = function(e){ 
			var ev = document.all ? window.event : e; 
				if(ev.keyCode == 13) {//监听回车键
					userLogin();
				}
			}
}

function findPwd(){
	msg = "找回密码";
	var loginName = $("#userName").val();
	if (loginName == "" || loginName == null || loginName == undefined){
		layer.msg("请输入用户名！",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	} else {
		$.ajax({
			url:ctx+"/isUser",
			type:"POST",
			data:{loginName:loginName},
			cache : false,
			dataType : 'json',
			success:function(result){
					if(result.success){
						if(result.obj == loginName){
							layer.msg("该用户名不存在，请重新输入！",{
								icon: 3,
								time: 2000 //2秒关闭（如果不配置，默认是3秒）
							});
						} else {
							url = ctx + "/findPassword?loginName=" + loginName ;
							index = layer.open({
								type: 2,
								//skin: 'layui-layer-lan',
								title: msg,
								fix: false,
								shadeClose: true,
								scrollbar: false,
								maxmin: false,
								edit: true,
								closeBtn: 1,
								area: ['530px', '312px'],
								content: url,
								btn: ['确定', '取消']
							,yes: function(index, layero){ //或者使用btn1
								//按钮【按钮一】的回调
								var iframeWin = window[layero.find('iframe')[0]['name']]; 
								iframeWin.savePassword();
								
							},cancel: function(index){ //或者使用btn2
								//按钮【按钮二】的回调
							}
							});
						}
					} else {
						layer.msg(result.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				}
		});
		
}
}